﻿'******************************* File Header *********************************
'* File Name:    WmiCommandTest.vb
'* Project:      WmiConnection.Test
'* Copyright (c) Freddy Kuehne
'*
'* Unit tests for WmiConnection.
'* 
'* This source is subject to the GNU Library General Public License (LGPL).
'* See http://wmiconnection.codeplex.com/license.
'* All other rights reserved.
'*****************************************************************************
Imports System.Text
Imports System.Data.Common

<TestClass()>
Public Class WmiCommandTest
#Region "Tests"
  <TestMethod()>
  Public Sub HierarchyTest()
    Using con As New WmiConnection
      Using target As IDbCommand = con.CreateCommand()
        Assert.IsTrue(GetType(DbCommand).IsInstanceOfType(target))
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub CancelTest()
    Using con As New WmiConnection
      Using target As IDbCommand = con.CreateCommand()
        ' no exception
        target.Cancel()
        target.Cancel()
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub CommandTextTest()
    Using con As New WmiConnection
      Using target As IDbCommand = con.CreateCommand()
        Assert.IsTrue(String.IsNullOrEmpty(target.CommandText))
        target.CommandText = "SELECT 1"
        Assert.AreEqual("SELECT 1", target.CommandText)
        target.CommandText = Nothing
        Assert.IsTrue(String.IsNullOrEmpty(target.CommandText))
        target.CommandText = String.Empty
        Assert.IsTrue(String.IsNullOrEmpty(target.CommandText))
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub CommandTimeoutTest()
    Using con As New WmiConnection
      Using target As IDbCommand = con.CreateCommand()
        Assert.AreEqual(30, target.CommandTimeout)
        target.CommandTimeout = 120
        Assert.AreEqual(120, target.CommandTimeout)
        target.CommandTimeout = 0
        Assert.AreEqual(0, target.CommandTimeout)
      End Using
    End Using
  End Sub

  <TestMethod(), ExpectedException(GetType(ArgumentOutOfRangeException))>
  Public Sub CommandTimeoutExceptionTest()
    Using con As New WmiConnection
      Using target As IDbCommand = con.CreateCommand()
        target.CommandTimeout = -1
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub CommandTypeTest()
    Using con As New WmiConnection
      Using target As IDbCommand = con.CreateCommand()
        For Each en As CommandType In [Enum].GetValues(GetType(CommandType))
          target.CommandType = en
          Assert.AreEqual(en, target.CommandType)
        Next
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub ConnectionTest()
    Using con As New WmiConnection
      Using target As IDbCommand = con.CreateCommand()
        Assert.AreSame(con, target.Connection)
        target.Connection = Nothing
        Assert.IsNull(target.Connection)
        target.Connection = con
        Assert.AreSame(con, target.Connection)
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub CreateCommandTest()
    Dim param As IDbDataParameter

    Using con As New WmiConnection
      Using target As IDbCommand = con.CreateCommand()
        param = target.CreateParameter
        Assert.IsNotNull(param)
        Assert.IsTrue(GetType(WmiParameter).IsInstanceOfType(param))
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub ExecuteNonQueryTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using target As IDbCommand = con.CreateCommand()
        Assert.AreEqual(0, target.ExecuteNonQuery)
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub ExecuteReaderTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using target As IDbCommand = con.CreateCommand()
        target.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Using rd As IDataReader = target.ExecuteReader
          Assert.IsNotNull(rd)
          Assert.IsTrue(GetType(WmiDataReader).IsInstanceOfType(rd))
          rd.Close()
        End Using
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub ExecuteScalarTest()
    Dim csb As New WmiConnectionStringBuilder("localhost", "\root\cimv2", Nothing, Nothing)
    Using con As New WmiConnection(csb.ConnectionString)
      con.Open()
      Using target As IDbCommand = con.CreateCommand()
        target.CommandText = "SELECT Version FROM Win32_OperatingSystem"
        Assert.AreEqual(con.ServerVersion, target.ExecuteScalar)
      End Using
    End Using
  End Sub

  <TestMethod()>
  Public Sub PrepareTest()
    Using con As New WmiConnection
      Using target As IDbCommand = con.CreateCommand()
        target.CommandText = "SELECT Version FROM Win32_OperatingSystem"

        ' no exception
        target.Prepare()
        target.Prepare()
      End Using
    End Using
  End Sub

#End Region
End Class
